import cn.hutool.core.util.ObjectUtil;
import constant.ConfigConstants;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import tool.*;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.image.ImageView;

/**
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/4
 */
public class Main extends Application {

    private static Media media;

    private static MediaPlayer mediaPlayer;

    private static ImageView imageView;

    private static Label nameLabel;

    private static Label artistsNameLabel;

    private static Button nextButton;

    public static void main(String[] args) {
        Application.launch();
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("生活小工具");
        primaryStage.setFullScreen(ConfigConstants.FullScreen);

        GridPane gridPane = new GridPane();
        gridPane.setVgap(10); // 设置行间距
        gridPane.setHgap(10); // 设置列间距
        gridPane.setPadding(new Insets(10)); // 设置边距

        // 创建音乐播放器
        media = new Media(Music.musicMap.get("url"));
        mediaPlayer = new MediaPlayer(media);
        // 创建图片视图
        imageView = new ImageView(new Image(Music.musicMap.get("picurl")));
        imageView.setId("musicImage");
        imageView.setFitWidth(130);
        imageView.setFitHeight(100);
        imageView.setPreserveRatio(false);
        gridPane.add(imageView, 160, 0);
        GridPane.setColumnSpan(imageView, 3); // 将图片所在的列跨越3列

        nameLabel = new Label();
        nameLabel.setWrapText(true);
        nameLabel.setText("歌曲名："+ Music.musicMap.get("name"));
        gridPane.add(nameLabel, 160, 2);
        GridPane.setColumnSpan(nameLabel, 3); // 将图片所在的列跨越3列
        artistsNameLabel = new Label();
        artistsNameLabel.setText("演唱者："+ Music.musicMap.get("artistsname"));
        gridPane.add(artistsNameLabel, 160, 3);
        GridPane.setColumnSpan(artistsNameLabel, 3); // 将图片所在的列跨越3列

        // 创建播放按钮
        Button playButton = new Button("播放");
        gridPane.add(playButton, 160, 1);
        playButton.setOnAction(event -> {
            mediaPlayer.play();
        });
        // 创建停止按钮
        Button stopButton = new Button("暂停");
        gridPane.add(stopButton, 161, 1);
        GridPane.setHalignment(stopButton, HPos.CENTER);
        stopButton.setOnAction(event -> mediaPlayer.stop());
        // 创建停止按钮
        nextButton = new Button("下首");
        gridPane.add(nextButton, 162, 1);
        nextButton.setOnAction(event -> {
            Music.getMusic();
            mediaPlayer.stop();
            media = new Media(Music.musicMap.get("url"));
            mediaPlayer = new MediaPlayer(media);
            imageView = (ImageView) gridPane.lookup("#musicImage");
            if (ObjectUtil.isNotNull(imageView)) {
                imageView.setImage(new Image(Music.musicMap.get("picurl")));
            }
            imageView.setFitWidth(150);
            imageView.setFitHeight(150);
            imageView.setPreserveRatio(false);
            nameLabel.setText("歌曲名："+ Music.musicMap.get("name"));
            artistsNameLabel.setText("演唱者："+ Music.musicMap.get("artistsname"));
            mediaPlayer.play();
        });
        //歌曲放完自动点击播放下一首
        mediaPlayer.setOnEndOfMedia(() ->{
            Music.getMusic();
            mediaPlayer.stop();
            media = new Media(Music.musicMap.get("url"));
            mediaPlayer = new MediaPlayer(media);
            imageView = (ImageView) gridPane.lookup("#musicImage");
            if (ObjectUtil.isNotNull(imageView)) {
                imageView.setImage(new Image(Music.musicMap.get("picurl")));
            }
            imageView.setFitWidth(150);
            imageView.setFitHeight(150);
            imageView.setPreserveRatio(false);
            nameLabel.setText("歌曲名："+ Music.musicMap.get("name"));
            artistsNameLabel.setText("演唱者："+ Music.musicMap.get("artistsname"));
            mediaPlayer.play();
        });

        // 创建主界面的按钮
        // 创建天气按钮
        Button weatherButton = new Button();
        weatherButton.getStyleClass().add("weather-button");
        weatherButton.setOnAction(event -> Weather.openWeatherStage());
        weatherButton.setOnMouseEntered(event -> {
            weatherButton.getStyleClass().add("weather-button-hover");
            Tooltip.install(weatherButton, new Tooltip("天气查询"));
        });
        weatherButton.setOnMouseExited(event -> {
            weatherButton.getStyleClass().remove("weather-button-hover");
            Tooltip.uninstall(weatherButton, new Tooltip("天气查询"));
        });
        Label weatherLabel = new Label("    天气");
        gridPane.add(weatherButton, 0, 0);
        gridPane.add(weatherLabel, 0, 1);

        // 创建快递按钮
        Button expressButton = new Button();
        expressButton.getStyleClass().add("express-button");
        expressButton.setOnAction(event -> Express.openExpressStage());
        expressButton.setOnMouseEntered(event -> {
            expressButton.getStyleClass().add("express-button-hover");
            Tooltip.install(expressButton, new Tooltip("快递查询"));
        });
        expressButton.setOnMouseExited(event -> {
            expressButton.getStyleClass().remove("express-button-hover");
            Tooltip.uninstall(expressButton, new Tooltip("快递查询"));
        });
        Label expressLabel = new Label("    快递");
        gridPane.add(expressButton, 0, 2);
        gridPane.add(expressLabel, 0, 3);

        // 创建查询星座按钮
        Button constellationButton = new Button();
        constellationButton.getStyleClass().add("constellation-button");
        constellationButton.setOnAction(event -> Constellation.openConstellationStage());
        constellationButton.setOnMouseEntered(event -> {
            constellationButton.getStyleClass().add("constellation-button-hover");
            Tooltip.install(constellationButton, new Tooltip("星座查询"));
        });
        constellationButton.setOnMouseExited(event -> {
            constellationButton.getStyleClass().remove("constellation-button-hover");
            Tooltip.uninstall(constellationButton, new Tooltip("星座查询"));
        });
        Label constellationLabel = new Label("    星座");
        gridPane.add(constellationButton, 0, 4);
        gridPane.add(constellationLabel, 0, 5);

        // 创建查询身高体重按钮
        Button statureButton = new Button();
        statureButton.getStyleClass().add("stature-button");
        statureButton.setOnAction(event -> Stature.openStatureStage());
        statureButton.setOnMouseEntered(event -> {
            statureButton.getStyleClass().add("stature-button-hover");
            Tooltip.install(statureButton, new Tooltip("身高体重计算"));
        });
        statureButton.setOnMouseExited(event -> {
            statureButton.getStyleClass().remove("stature-button-hover");
            Tooltip.uninstall(statureButton, new Tooltip("身高体重计算"));
        });
        Label statureLabel = new Label("  身高体重");
        gridPane.add(statureButton, 0, 6);
        gridPane.add(statureLabel, 0, 7);

        // 创建json格式化按钮
        Button jsonButton = new Button();
        jsonButton.getStyleClass().add("json-button");
        jsonButton.setOnAction(event -> Json.openJsonStage());
        jsonButton.setOnMouseEntered(event -> {
            jsonButton.getStyleClass().add("json-button-hover");
            Tooltip.install(jsonButton, new Tooltip("JSON格式化"));
        });
        jsonButton.setOnMouseExited(event -> {
            jsonButton.getStyleClass().remove("json-button-hover");
            Tooltip.uninstall(jsonButton, new Tooltip("JSON格式化"));
        });
        Label jsonLabel = new Label("JSON格式化");
        gridPane.add(jsonButton, 0, 8);
        gridPane.add(jsonLabel, 0, 9);


        // 创建代码生成器按钮
        Button generatorButton = new Button();
        generatorButton.getStyleClass().add("generator-button");
        generatorButton.setOnAction(event -> Generator.openCodeGeneratorStage());
        generatorButton.setOnMouseEntered(event -> {
            generatorButton.getStyleClass().add("generator-button-hover");
            Tooltip.install(generatorButton, new Tooltip("代码生成器"));
        });
        generatorButton.setOnMouseExited(event -> {
            generatorButton.getStyleClass().remove("generator-button-hover");
            Tooltip.uninstall(generatorButton, new Tooltip("代码生成器"));
        });
        Label generatorLabel = new Label(" 代码生成器");
        gridPane.add(generatorButton, 0, 10);
        gridPane.add(generatorLabel, 0, 11);

        // 创建ocr图片识别按钮
        Button ocrButton = new Button();
        ocrButton.getStyleClass().add("ocr-button");
        ocrButton.setOnAction(event -> Ocr.openOCRStage());
        ocrButton.setOnMouseEntered(event -> {
            ocrButton.getStyleClass().add("ocr-button-hover");
            Tooltip.install(ocrButton, new Tooltip("OCR文字识别"));
        });
        ocrButton.setOnMouseExited(event -> {
            ocrButton.getStyleClass().remove("ocr-button-hover");
            Tooltip.uninstall(ocrButton, new Tooltip("OCR文字识别"));
        });
        Label ocrLabel = new Label(" OCR文字识别");
        gridPane.add(ocrButton, 0, 12);
        gridPane.add(ocrLabel, 0, 13);

        // 创建ASCII转换按钮
        Button asciiButton = new Button();
        asciiButton.getStyleClass().add("ascii-button");
        asciiButton.setOnAction(event -> ASCII.openASCIIStage());
        asciiButton.setOnMouseEntered(event -> {
            asciiButton.getStyleClass().add("ascii-button-hover");
            Tooltip.install(asciiButton, new Tooltip("ASCII转换"));
        });
        asciiButton.setOnMouseExited(event -> {
            asciiButton.getStyleClass().remove("ascii-button-hover");
            Tooltip.uninstall(asciiButton, new Tooltip("ASCII转换"));
        });
        Label asciiLabel = new Label(" ASCII转换");
        gridPane.add(asciiButton, 0, 14);
        gridPane.add(asciiLabel, 0, 16);

        // 创建热搜按钮
        Button topButton = new Button();
        topButton.getStyleClass().add("top-button");
        topButton.setOnAction(event -> TopHot.openTopStage());
        topButton.setOnMouseEntered(event -> {
            topButton.getStyleClass().add("top-button-hover");
            Tooltip.install(topButton, new Tooltip("热搜"));
        });
        topButton.setOnMouseExited(event -> {
            topButton.getStyleClass().remove("top-button-hover");
            Tooltip.uninstall(topButton, new Tooltip("热搜"));
        });
        Label topLabel = new Label(" 热搜");
        gridPane.add(topButton, 2, 0);
        gridPane.add(topLabel, 2, 1);

        // 创建主界面的布局
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().add(gridPane);

        Image backgroundImage = new Image(this.getClass().getResourceAsStream("/images/background.jpg"));
        BackgroundImage backgroundImg = new BackgroundImage(backgroundImage, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.DEFAULT, new BackgroundSize(800, 700, true, true, true, true));
        Background background = new Background(backgroundImg);
        root.setBackground(background);
        // 创建主界面的场景
        Scene scene = new Scene(root, 1200, 850);
        scene.getStylesheets().add(getClass().getResource("/css/main.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
